package com.gzrobot;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamUtils;
import com.github.sarxos.webcam.util.ImageUtils;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.*;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

/**
 * Hello world!
 */
@Slf4j
public class App {
    public static void main(String[] args) {

        List<Webcam> webcams = Webcam.getWebcams();
        for (Webcam webcam : webcams) {
            System.out.println("初始化 " + webcam.getName());
            webcam.setCustomViewSizes(new Dimension(4656, 3496));
            webcam.setViewSize(new Dimension(4656, 3496));
            webcam.open();
        }

        while(true){
            String input = new Scanner(System.in).nextLine();
            if (Objects.equals(input,"2")){
                return;
            }
            if (Objects.equals(input, "1")){
                for (Webcam webcam : webcams) {
                    capturePref(webcam,0.8f);
                    capturePref(webcam,0.85f);
                    capturePref(webcam,0.9f);
                    capturePref(webcam,0.95f);
                    capturePref(webcam,1.0f);
                }
            }
        }
    }

    public static void capturePref(Webcam webcam,Float f) {
        try {
            String name = webcam.getDevice().getName();
            System.out.println("截图设备名称:"+name);

            Dimension viewSize = webcam.getViewSize();
            System.out.println("实际分辨率"+viewSize.width+"x"+viewSize.height);

            long begin = System.currentTimeMillis();

            BufferedImage image = webcam.getImage();

            File outputFile = new File("D:\\" + name + System.currentTimeMillis() + ".jpg");
            //ImageIO.write(image, "JPEG", new File("D:\\"+name+System.currentTimeMillis() + ".jpg"));

            ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
            try (ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile)) {
                writer.setOutput(ios);

                // 参数配置
                JPEGImageWriteParam param = new JPEGImageWriteParam(null);
                param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                param.setCompressionQuality(f);

                // 写入图像
                writer.write(null, new IIOImage(image, null, null), param);
            } finally {
                writer.dispose();
            }

            long endTime = System.currentTimeMillis();
            System.out.println("costTime:"+(endTime-begin));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void captureOld(Webcam webcam) {
        try {
            String name = webcam.getDevice().getName();
            System.out.println("截图设备名称:"+name);

            Dimension viewSize = webcam.getViewSize();
            System.out.println("实际分辨率"+viewSize.width+"x"+viewSize.height);

            long begin = System.currentTimeMillis();
            BufferedImage image = webcam.getImage();
            ImageIO.write(image, "JPG", new File("D:\\old-"+name+System.currentTimeMillis() + ".jpg"));
            long endTime = System.currentTimeMillis();
            System.out.println("costTime:"+(endTime-begin));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void captureOld2(Webcam webcam) {
        try {
            String name = webcam.getDevice().getName();
            System.out.println("截图设备名称:"+name);

            Dimension viewSize = webcam.getViewSize();
            System.out.println("实际分辨率"+viewSize.width+"x"+viewSize.height);

            long begin = System.currentTimeMillis();
            WebcamUtils.capture(webcam,new File("D:\\old-"+name+System.currentTimeMillis() + ".jpg"), ImageUtils.FORMAT_PNG);
            long endTime = System.currentTimeMillis();
            System.out.println("costTime:"+(endTime-begin));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }






}
